package com.cucn.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cucn.commonutils.JwtUtils;
import com.cucn.commonutils.MD5;
import com.cucn.educenter.entity.RegisterVO;
import com.cucn.educenter.entity.UcenterMember;
import com.cucn.educenter.mapper.UcenterMemberMapper;
import com.cucn.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cucn.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author KouChaoJie
 * @since 2021-10-10
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 用户登录方法
     *
     * @param member 登录用户信息
     * @return token
     */
    @Override
    public String login(UcenterMember member) {
        //获取登录用户的手机号,密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "手机号或密码错误!");
        }

        //判断手机号正确与否
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查出的对象是否为空
        if (mobileMember == null) {
            throw new GuliException(20001, "该手机号不存在!");
        }

        //判断密码
        //因为存储到数据库的密码肯定是加密的
        //把输入的密码先进行加密,再和数据库密码进行比较
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuliException(20001, "密码错误!");
        }

        //判断用户是否被禁用
        if (mobileMember.getIsDisabled()) {
            throw new GuliException(20001, "用户被禁用!");
        }

        //登录成功
        //生成token,使用jwt工具类
        return JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
    }

    /**
     * 用户注册方法
     *
     * @param registerVO 注册信息传输对象
     */
    @Override
    public void register(RegisterVO registerVO) {
        //获取注册数据
        //验证码
        String code = registerVO.getCode();
        //手机号
        String mobile = registerVO.getMobile();
        //昵称
        String nickname = registerVO.getNickname();
        //密码
        String password = registerVO.getPassword();

        //判断
        if (StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(nickname)) {
            throw new GuliException(20001, "注册失败!");
        }

        //判断验证码
        //获取redis里的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new GuliException(20001, "验证码错误!");
        }

        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "手机号已被注册!");
        }

        //数据添加到数据库中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setIsDisabled(false);
        //默认头像
        member.setAvatar("https://edu-kou1010.oss-cn-shanghai.aliyuncs.com/2021/09/28/f012a76086bb42409d3b0f12d758a684file.png");
        baseMapper.insert(member);
    }

    /**
     * 根据微信openid查询用户
     *
     * @param openid 用户openid
     * @return 用户对象
     */
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 根据日期查询注册人数
     *
     * @param day 日期
     * @return 注册人数
     */
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
